package edu.del.teamknowledgebase.service.code;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class CodeSendStrategyFactory {
    /**
     * 策略映射表，用于存储不同类型的代码发送策略
     * key为策略类型标识符，value为对应的代码发送策略实现
     */
    private final Map<String, CodeSendStrategy> strategyMap;

    public CodeSendStrategyFactory(List<CodeSendStrategy> strategies) {
        // 将所有策略按类型存入Map
        this.strategyMap = strategies.stream()
            .collect(Collectors.toMap(
                CodeSendStrategy::getType,
                Function.identity()
            ));
    }

    /**
     * 根据类型标识符获取对应的代码发送策略实现
     *
     * @param type 类型标识符
     * @return 对应的代码发送策略实现
     */
    public CodeSendStrategy getStrategy(String type) {
        CodeSendStrategy sendStrategy = strategyMap.get(type);
        if (sendStrategy == null) {
            throw new IllegalArgumentException("Invalid verification code type: " + type);
        }
        return sendStrategy;
    }
}
